package main;
// <editor-fold defaultstate="collapsed" desc="import">
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JOptionPane;
// </editor-fold>
public class Frame extends javax.swing.JFrame {
    // <editor-fold defaultstate="collapsed" desc="main()"> 
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Frame().setVisible(true);
            }
        });
    }
    */
// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Frame(Main main)"> 
    public Frame(Main main) {
        this.main=main;
        loginPanel = new javax.swing.JPanel();
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(350, 248));
        setResizable(false);

        loginPanel.setBackground(new java.awt.Color(0, 0, 0));
        loginPanel.setForeground(new java.awt.Color(255, 255, 255));
        loginPanel.setPreferredSize(new java.awt.Dimension(350, 218));
        initLoginPanel();
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Frame()"> 
    private Frame() {
        throw new UnsupportedOperationException("Not yet implemented");
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="initLoginPanel">
    private void initLoginPanel() {
        loginLabel = new javax.swing.JLabel();
        usernameLabel = new javax.swing.JLabel();
        passLabel = new javax.swing.JLabel();
        userTextField = new javax.swing.JTextField();
        passTextField = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JButton();
        exitLoginButton = new javax.swing.JButton();
        regButton = new javax.swing.JButton();
        ajaxField = new javax.swing.JLabel();
        
        userTextField.setText("desune");
        passTextField.setText("desune");
        loginLabel.setBackground(new java.awt.Color(0, 0, 0));
        loginLabel.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        loginLabel.setForeground(new java.awt.Color(255, 255, 255));
        loginLabel.setText("Đăng Nhập Trò Chơi Tetris");

        usernameLabel.setBackground(new java.awt.Color(0, 0, 0));
        usernameLabel.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        usernameLabel.setForeground(new java.awt.Color(255, 255, 255));
        usernameLabel.setText("Tài Khoản");

        passLabel.setBackground(new java.awt.Color(0, 0, 0));
        passLabel.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        passLabel.setForeground(new java.awt.Color(255, 255, 255));
        passLabel.setText("Mật Khẩu");

        loginButton.setText("Đăng Nhập");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        exitLoginButton.setText("Thoát");
        exitLoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitLoginButtonActionPerformed(evt);
            }
        });

        regButton.setText("Đăng Ký");
        regButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regButtonActionPerformed(evt);
            }
        });

        ajaxField.setBackground(new java.awt.Color(0, 0, 0));
        ajaxField.setForeground(new java.awt.Color(255, 0, 0));
        ajaxField.setText("Hãy Nhập username và password");
        
        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(exitLoginButton, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(regButton, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(usernameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                            .addComponent(passLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(userTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(passTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(ajaxField, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(loginLabel)
                .addGap(78, 78, 78))
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginLabel)
                .addGap(41, 41, 41)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(userTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passLabel)
                    .addComponent(passTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ajaxField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginButton)
                    .addComponent(exitLoginButton)
                    .addComponent(regButton))
                .addGap(37, 37, 37))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }//</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="initChoosetypePanel">
    
    public void initChoosetypePanel(){
        singleButton = new javax.swing.JButton();
        multiButton = new javax.swing.JButton();
        exitChooseTypeButton = new javax.swing.JButton();

        singleButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        singleButton.setText("Một Người Chơi");
        singleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                singleButtonActionPerformed(evt);
            }
        });

        multiButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        multiButton.setText("Nhiều Người Chơi");
        multiButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multiButtonActionPerformed(evt);
            }
        });

        exitChooseTypeButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        exitChooseTypeButton.setText("Thoát");
        exitChooseTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout chooseTypePanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(chooseTypePanelLayout);
        chooseTypePanelLayout.setHorizontalGroup(
            chooseTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, chooseTypePanelLayout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addGroup(chooseTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(exitChooseTypeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(multiButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(singleButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(96, 96, 96))
        );
        chooseTypePanelLayout.setVerticalGroup(
            chooseTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseTypePanelLayout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(singleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(multiButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(exitChooseTypeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }//</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="initChooseRoomPanel">
    
    public void initChooseRoomPanel(){
        
        RoomListLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        RoomList = new javax.swing.JList();
        timPhongButton = new javax.swing.JButton();
        TaoPhongButton = new javax.swing.JButton();
        ThamGiaButton = new javax.swing.JButton();
        exitChooseRoomButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));
        setForeground(java.awt.Color.white);
        setPreferredSize(new java.awt.Dimension(350, 248));

        RoomListLabel.setBackground(new java.awt.Color(0, 0, 0));
        RoomListLabel.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        RoomListLabel.setForeground(new java.awt.Color(255, 255, 255));
        RoomListLabel.setText("Danh Sách Phòng Chơi");

        RoomList.setBackground(new java.awt.Color(0, 0, 0));
        RoomList.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        RoomList.setForeground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setViewportView(RoomList);

        timPhongButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        timPhongButton.setText("Tìm Phòng");
        timPhongButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timPhongButtonActionPerformed(evt);
            }
        });

        TaoPhongButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        TaoPhongButton.setText("Tạo Phòng");
        TaoPhongButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TaoPhongButtonActionPerformed(evt);
            }
        });

        ThamGiaButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        ThamGiaButton.setText("Tham Gia");
        ThamGiaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ThamGiaButtonActionPerformed(evt);
            }
        });

        exitChooseRoomButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        exitChooseRoomButton.setText("Thoát");
        exitChooseRoomButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitChooseRoomButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout chooseRoomPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(chooseRoomPanelLayout);
        chooseRoomPanelLayout.setHorizontalGroup(
            chooseRoomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseRoomPanelLayout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addComponent(RoomListLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, chooseRoomPanelLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(chooseRoomPanelLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(ThamGiaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(chooseRoomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(timPhongButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(chooseRoomPanelLayout.createSequentialGroup()
                        .addComponent(exitChooseRoomButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TaoPhongButton)))
                .addGap(0, 23, Short.MAX_VALUE))
        );
        chooseRoomPanelLayout.setVerticalGroup(
            chooseRoomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseRoomPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(RoomListLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(timPhongButton)
                .addGap(10, 10, 10)
                .addGroup(chooseRoomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exitChooseRoomButton)
                    .addComponent(TaoPhongButton)
                    .addComponent(ThamGiaButton))
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="initRegisterPanel">
    public void initRegisterPanel(){
        //regPanel = new javax.swing.JPanel();
        regLabel = new javax.swing.JLabel();
        regUsernameLabel = new javax.swing.JLabel();
        regPassLabel = new javax.swing.JLabel();
        regPass2Label = new javax.swing.JLabel();
        DangKyButton = new javax.swing.JButton();
        BackButton = new javax.swing.JButton();
        regUsernameTextField = new javax.swing.JTextField();
        regPassTextField = new javax.swing.JPasswordField();
        regPass2TextField = new javax.swing.JPasswordField();

        regLabel.setBackground(new java.awt.Color(0, 0, 0));
        regLabel.setForeground(new java.awt.Color(255, 255, 255));
        regLabel.setText("ĐĂNG KÍ TÀI KHOẢN");

        regUsernameLabel.setForeground(new java.awt.Color(255, 255, 255));
        regUsernameLabel.setText("Tên đăng nhập");

        regPassLabel.setForeground(new java.awt.Color(255, 255, 255));
        regPassLabel.setText("Mật khẩu");

        regPass2Label.setForeground(new java.awt.Color(255, 255, 255));
        regPass2Label.setText("Xác nhận mật khẩu");

        DangKyButton.setText("ĐĂNG KÍ");
        DangKyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DangKyButtonActionPerformed(evt);
            }
        });

        BackButton.setText("QUAY LẠI");
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HuyDangKyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout regPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(regPanelLayout);
        regPanelLayout.setHorizontalGroup(
            regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(regPanelLayout.createSequentialGroup()
                .addContainerGap(38, Short.MAX_VALUE)
                .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, regPanelLayout.createSequentialGroup()
                        .addComponent(regLabel)
                        .addGap(120, 120, 120))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, regPanelLayout.createSequentialGroup()
                        .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(regPassLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(regPass2Label, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(regUsernameLabel))
                        .addGap(18, 18, 18)
                        .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(regPassTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(regUsernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(regPass2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(regPanelLayout.createSequentialGroup()
                                .addComponent(DangKyButton)
                                .addGap(18, 18, 18)
                                .addComponent(BackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(41, 41, 41))))
        );
        regPanelLayout.setVerticalGroup(
            regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(regPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(regLabel)
                .addGap(35, 35, 35)
                .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regUsernameLabel)
                    .addComponent(regUsernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regPassTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(regPassLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regPass2Label)
                    .addComponent(regPass2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DangKyButton)
                    .addComponent(BackButton))
                .addContainerGap(65, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="RegisterMethods">
    private void HuyDangKyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //this.setVisible(false);
        loginPanel.removeAll();
        initLoginPanel();
    }                                        

    private void DangKyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String user=regUsernameTextField.getText();
        String pass=regPassTextField.getText();
        String pass2=regPass2TextField.getText();
        if(user==null||user.equals(""))
        {
            regUsernameTextField.setForeground(new java.awt.Color(255, 0, 0));
            regUsernameTextField.setText("Vui lòng nhập tên đăng nhập");
            return;
        }
        if(pass==null||pass.equals(""))
        {
            regLabel.setText("Vui lòng nhập password");
            return;
        }
        if(pass.equals(pass2)==false)
        {
            regLabel.setText("Mật khẩu chưa đúng");
            return;
        }
        try{
            Socket skt=new Socket(main.host, main.port);
            PrintWriter out=new PrintWriter(skt.getOutputStream(),true);
            BufferedReader in=new BufferedReader(new InputStreamReader(skt.getInputStream()));
            out.println("6|"+user+"|"+pass);
            String input=in.readLine();
            if(input.equals("1"))
            {
                regLabel.setText("ĐĂNG KÍ TÀI KHOẢN THÀNH CÔNG");
                //this.setVisible(false);
            }else{
                regLabel.setText("KHÔNG THỂ TẠO TÀI KHOẢN");
                
            }
        }catch(Exception e)
        {
            System.err.print(e);
        }
    }          
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="ChooseRoomMethods">
    
    private void exitChooseRoomButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        loginPanel.removeAll();
        initChoosetypePanel();
    }
    private void timPhongButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        main.showgame(this);
    }                                        
    public void showroomlist(String list)
    {
        char[] s=list.toCharArray();
        int j=0;
        final String[] room=new String[5];
        int index=0;
        for(int i=1;i<list.length();i++)
        {
            if(s[i]=='|')
            {
               room[index++]=list.substring(j,i);
               j=i+1;
            }
        }
        
        RoomList.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return room.length; }
            public Object getElementAt(int i) { return room[i]; }
        });

    }
    private void ThamGiaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String roomname=(String)RoomList.getSelectedValue();
        if(roomname!=null&&roomname.equals("")==false)
        {
        main.joingame(roomname,this);
        }
    }                                        

    private void TaoPhongButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        main.createroom(this);
        this.dispose();

    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="ChooseTypeMethods">
    
    private void singleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        main.singleplayer(this);
    }                                        

    private void multiButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        loginPanel.removeAll();
        initChooseRoomPanel();
    }                                        

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("\n" + main.username +" Logout");
        main.logout();
        this.dispose();
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="LoginMethods">
    private void regButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        loginPanel.removeAll();
        initRegisterPanel();
    }                                        

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        int i=checkfield();
        if(i==1)login();
        else if(i==2)
        {
            //JOptionPane.showMessageDialog(null, "ban phai nhap tai khoan");
            ajaxField.setText("Chưa nhập username!");
            passTextField.setText(null);
        }else if(i==3)
        {
            //JOptionPane.showMessageDialog(null, "ban phai nhap mat khau");
            ajaxField.setText("Chưa nhập password!");
            userTextField.setText(null);
        }

    }                                        

    private void exitLoginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.exit(1);
    }                                        
    
    private int checkfield()
    {
        String user=userTextField.getText();
        if(user==null||user.equals(""))return 2;
        String pass=passTextField.getText();
        if(pass==null||pass.equals(""))return 3;
        return 1;
    }
    private void login()
    {
        String user=userTextField.getText().toLowerCase();
        String pass=passTextField.getText();
        main.login(user,pass);

    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variables declaration">
    public Main main;
    public PrintWriter out;
    private javax.swing.JList RoomList;
    private javax.swing.JLabel RoomListLabel;
    private javax.swing.JButton TaoPhongButton;
    private javax.swing.JButton ThamGiaButton;
    //private javax.swing.JPanel chooseRoomPanel;
    public javax.swing.JButton exitChooseRoomButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton timPhongButton;
    
    private javax.swing.JButton DangKyButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JLabel regLabel;
    //private javax.swing.JPanel regPanel;
    private javax.swing.JLabel regPass2Label;
    private javax.swing.JPasswordField regPass2TextField;
    private javax.swing.JLabel regPassLabel;
    private javax.swing.JPasswordField regPassTextField;
    private javax.swing.JLabel regUsernameLabel;
    private javax.swing.JTextField regUsernameTextField;
    
    private javax.swing.JButton exitChooseTypeButton;
    private javax.swing.JButton multiButton;
    public javax.swing.JButton singleButton;
    // Variables declaration - do not modify
    private javax.swing.JButton exitLoginButton;
    private javax.swing.JButton loginButton;
    private javax.swing.JLabel loginLabel;
    public javax.swing.JPanel loginPanel;
    private javax.swing.JLabel passLabel;
    private javax.swing.JPasswordField passTextField;
    private javax.swing.JButton regButton;
    private javax.swing.JTextField userTextField;
    private javax.swing.JLabel usernameLabel;
    public javax.swing.JLabel ajaxField;
    // End of variables declaration
    //</editor-fold> 
}
